package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productmanager.service.QueryProductTemplateCall;
import com.ymatou.iapi.productmanager.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.sqlwapper.YmtProductCategoryWapper;
import com.ymttest.database.sqlwapper.YmtProductDescExtraWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_QueryProductTemplate {
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static QueryProductTemplateCall queryProductTemplateCall = new QueryProductTemplateCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商品模板列表");
		//SqlMapperFactory.getIntergratedProductMapper().updateProductoffSale(sellerId);
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryProductTemplateCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductTemplate_001() {
		Logger.start(true, "获取商品模板列表");
		try{
			int templateType = 1;
			
			
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			SellerProductCallService.deleteTemplate(sellerId);
			//create
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId,"TemplateType",templateType));
			Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			
			JSONObject ret1 = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId,"TemplateType",templateType));
			Logger.verifyEquals(200, ret1.getInt("Code"), "返回报文code");
			
			
			YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
			
			List<Map<String,Object>> retList = ymtProductDescExtraWapper.selectBySellerIdAndTypeId(sellerId, templateType);
			
			JSONObject retObject = queryProductTemplateCall.callAndGetReturnData(String.valueOf(sellerId), String.valueOf(templateType));
			
			Logger.verifyEquals(200, retObject.getInt("Code"), "报文返回值比对");
			
			checkRetOfProductTemplate(retList,retObject.getJSONObject("Data").getJSONArray("TemplateList"));
			
			Logger.verifyEquals(true, retObject.getJSONObject("Data").isNull("SamplePic"), "samplePic 应为空");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductTemplate_002() {
		Logger.start(true, "获取商品模板列表,品类图");
		try{
			int templateType = 1;
			int thirdCategoryId = 1338;
			
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			SellerProductCallService.deleteTemplate(sellerId);
			//create
			JSONObject ret = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId,"TemplateType",templateType));
			Logger.verifyEquals(200, ret.getInt("Code"), "返回报文code");
			
			JSONObject ret1 = saveProductTemplateCall.callAndGetReturnData(MapUtil.hashMap("SellerId", sellerId,"TemplateType",templateType));
			Logger.verifyEquals(200, ret1.getInt("Code"), "返回报文code");
			
			
			YmtProductDescExtraWapper ymtProductDescExtraWapper = new YmtProductDescExtraWapper();
			
			List<Map<String,Object>> retList = ymtProductDescExtraWapper.selectBySellerIdAndTypeId(sellerId, templateType);
			
			JSONObject retObject = queryProductTemplateCall.callAndGetReturnData(String.valueOf(sellerId), String.valueOf(templateType),thirdCategoryId);
			
			Logger.verifyEquals(200, retObject.getInt("Code"), "报文返回值比对");
			
			checkRetOfProductTemplate(retList,retObject.getJSONObject("Data").getJSONArray("TemplateList"));
			
			
			@SuppressWarnings("rawtypes")
			Map productCategoryMap = new YmtProductCategoryWapper().getProductCategoryByCategoryId(thirdCategoryId).get(0);
			
			Logger.verifyEquals(productCategoryMap.get("SamplePic").toString(), retObject.getJSONObject("Data").getString("SamplePic"), "samplePic 不应为空");
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	/***
	 * 
	 * @param templateInfosInSql sql中template值
	 * @param templateListInRet 接口请求返回的template值
	 * @throws JSONException 
	 */
	public static void checkRetOfProductTemplate(List<Map<String,Object>> templateInfosInSql, JSONArray templateListInRet) throws Exception{
		Logger.verifyEquals(templateInfosInSql.size(), templateListInRet.length(), "templatelist 接口返回个数应该与数据库个数相同");
		
		for(int i=0;i<templateListInRet.length();i++){
			JSONObject templateInRet = templateListInRet.getJSONObject(i);
			Integer templateId = templateInRet.getInt("ProductTemplateId");
			String templateName = templateInRet.getString("TemplateName");
			JSONArray picList = templateInRet.getJSONArray("PicUrl");
			
			Map<String, Object> templateInfoInSql = templateInfosInSql.stream().filter(ele->templateId.equals(Integer.parseInt(ele.get("ID").toString()))).findAny().orElse(null);
			
			Logger.verifyNotNull(templateInfoInSql, "Template数据库中应存在 templateid:"+ templateId);
			
			Logger.verifyEquals(templateInfoInSql.get("TemplateName").toString(), templateName, "TemplateName比较");
			
			//图片检查	
			if (templateInfoInSql.get("PicUrls")==null || templateInfoInSql.get("PicUrls").toString().equals("")){
				Logger.verifyEquals(0, picList.length(), "Template 图片应该不存在");
			}else{
				JSONArray templatePicsInfoInSql = new JSONArray(Arrays.asList(templateInfoInSql.get("PicUrls").toString().split(",")));
				AssertService.checkArray(templatePicsInfoInSql, picList,"Template 图片检查 ");
			}
		}
		
		
		
		
	}
	
	
	
	public static void main(String[] args) {
		List<Map<String, Object>> sqlProductDescDataList = new ArrayList<>();
		Map<String, Object> map = new HashMap<>();
		map.put("spid", "dwwerwerewrwerwe");
		map.put("pic", "dadfdaf_pic");
		map.put("desc", "desc_dfasd_");
		//sqlProductDescDataList.add(map);
		
		
		 Map<String, Object> tempDescMap = new HashMap<>();
         tempDescMap.putAll(sqlProductDescDataList.stream().findFirst().orElse(Collections.emptyMap()));
         tempDescMap.remove("pic");
         tempDescMap.put("descpics", sqlProductDescDataList.stream().map(x -> x.get("pic")).toArray());
         sqlProductDescDataList.clear();
         
         sqlProductDescDataList.add(tempDescMap);
         
	}
	
	
}
